<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/6/3
 * Time: 16:54
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\Log;

/**
 * 工资模型
 * 没有对应的实例化表格，仅仅作为处理和工资相关的数据模型
 * Class WageModel
 * @package App\Http\Model
 */
class WageModel extends Model
{
    /**
     * @param array $param
     * @return array
     * @throws \Exception
     */
    public function personalWage(array $param)
    {
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:date('Y-m-d 00:00:00', strtotime($param['startTime']));
        $endTime = empty($param['endTime'])?$findYear['endTime']:date('Y-m-d 23:59:59', strtotime($param['endTime']));

        $condition = [
            'employeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
        ];

        $workmanAccounts = (new WorkmanAccountsModel())->getAccountsList($condition);
        $handymanAccounts = (new HandymanAccountsModel())->getAccountsList($condition);
        $attendance = (new AttendanceModel())->getLists($condition);
        $loan = (new LoanModel())->getAccountsList($condition);

        $condition['type'] = LivingModel::RECHARGE;
        $livingRecharge = (new LivingModel())->getAccountsList($condition);
        $condition['type'] = LivingModel::REFUND;
        $livingRefund = (new LivingModel())->getAccountsList($condition);
        unset($condition['employeeId']);
        $condition['sourceEmployeeId'] = $param['id'];
        $condition['type'] = WarehouseLogModel::CONSUME;
        //材料费
        $materialAccounts = (new WarehouseLogInfoModel())->lists($condition);

        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }
        foreach ($wageList as $key => $w) {
            $wageList[$key] = [
                'livingRecharge' => 0,
                'livingRefund' => 0,
                'loan' => 0,
                'materialAccounts' => 0,
                'attendance' => 0,
                'workmanAccounts' => 0,
                'handymanAccounts' => 0,
            ];
        }
        $wageList = $this->wageList($wageList, $workmanAccounts, 'workmanAccounts', 'separateTime','account');
        $wageList = $this->wageList($wageList, $handymanAccounts, 'handymanAccounts', 'separateTime','account');
        $wageList = $this->wageList($wageList, $attendance, 'attendance', 'day', 'length');
        $wageList = $this->wageList($wageList, $loan, 'loan', 'loanTime','account');
        $wageList = $this->wageList($wageList, $livingRecharge, 'livingRecharge', 'livingTime','account');
        $wageList = $this->wageList($wageList, $livingRefund, 'livingRefund', 'livingTime','account');
        $wageList = $this->wageList($wageList, $materialAccounts, 'materialAccounts', 'time','totalPrice');
        return $wageList;
    }

    /**
     * 把列表按月分组
     * @param $wageList
     * @param $itemList
     * @param $item
     * @param $timeColumn
     * @param string $calcColumn
     * @return mixed
     * @throws \Exception
     */
    public function wageList($wageList, $itemList, $item, $timeColumn, $calcColumn = '')
    {
        foreach ($itemList as $i) {
            $month = (new \DateTime($i->$timeColumn))->format('Y-m');
            if ($calcColumn == ''){
                $wageList[$month][] = get_object_vars($i);
            }else{
                if (!isset($wageList[$month][$item])) {
                    $wageList[$month][$item] = 0;
                }
                $wageList[$month][$item] += $i->$calcColumn;
            }

        }
        return $wageList;
    }

    /**
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function personalWageWorkmanAccounts(array $param){
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:$param['startTime'].' 00:00:00';
        $endTime = empty($param['endTime'])?$findYear['endTime']:$param['endTime'].' 23:59:59';

        $condition = [
            'employeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
        ];

        $data = (new WorkmanAccountsModel())->getAccountsListWithInfo($condition);
        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }

        $result = $this->wageList($wageList, $data, 'workmanAccounts', 'separateTime');
        return $result;
    }

    /**
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function personalWageHandymanAccounts(array $param)
    {
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:$param['startTime'].' 00:00:00';
        $endTime = empty($param['endTime'])?$findYear['endTime']:$param['endTime'].' 23:59:59';

        $condition = [
            'employeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
        ];
        $data = (new HandymanAccountsModel())->getAccountsListWithInfo($condition);
        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }

        $result = $this->wageList($wageList, $data, 'handyman', 'separateTime');
        return $result;
    }

    /**
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function personalWageAttendance(array $param)
    {
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:$param['startTime'].' 00:00:00';
        $endTime = empty($param['endTime'])?$findYear['endTime']:$param['endTime'].' 23:59:59';

        $condition = [
            'employeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
        ];
        $attendance = (new AttendanceModel())->getLists($condition);
        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }

        $result = $this->wageList($wageList, $attendance, 'attendance', 'day');
        $total = 0;
        foreach ($result as $key => $r){
            $dayList = $this->datePeriod($key, $key, '1 day', 'j','attendance');
            if (!empty($r)){
                foreach ($r as $a) {
                    $d = (new \DateTime($a['day']))->format('j');
                    $dayList[$d]['long'] = empty($a['length'])?'':$a['length'];
                    $dayList[$d]['day'] = empty($a['day'])?'':$a['day'];
                    $dayList[$d]['projectName'] = empty($a['projectName'])?'':$a['projectName'];
                    $projectId = empty($a['projectId'])?'':$a['projectId'];
                    $dayList[$d]['projectId'] = $projectId;
                    $dayList[$d]['isCurrent'] = $a['isCurrent'];
                    $total += $a['length'];
                }
            }
            $result[$key] = $dayList;
        }
        //只为了凑够31天的空白单元格数据
        $dayList = $this->datePeriod('2020-01', '2020-01', '1 day', 'j','attendance');
        $dayList['total'] = $total;
        $result['total'] = $dayList;
        return $result;
    }

    /**
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function personalWageMaterialAccounts(array $param)
    {
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:$param['startTime'].' 00:00:00';
        $endTime = empty($param['endTime'])?$findYear['endTime']:$param['endTime'].' 23:59:59';

        $condition = [
            'sourceEmployeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
            'type' => WarehouseLogModel::CONSUME,
        ];
        $data = (new WarehouseLogInfoModel())->lists($condition);
        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }

        $result = $this->wageList($wageList, $data, 'materialAccounts', 'time');
        return $result;
    }

    /**
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function personalWageLoan(array $param)
    {
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:$param['startTime'].' 00:00:00';
        $endTime = empty($param['endTime'])?$findYear['endTime']:$param['endTime'].' 23:59:59';

        $condition = [
            'employeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
        ];
        $data = (new LoanModel())->lists($condition);
        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }

        $result = $this->wageList($wageList, $data, 'loan', 'loanTime');
        return $result;
    }

    /**
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function personalWageLivingRecharge(array $param)
    {
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:$param['startTime'].' 00:00:00';
        $endTime = empty($param['endTime'])?$findYear['endTime']:$param['endTime'].' 23:59:59';

        $condition = [
            'employeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
            'type'=>LivingModel::RECHARGE
        ];
        $data = (new LivingModel())->lists($condition);
        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }

        $result = $this->wageList($wageList, $data, 'livingRecharge', 'livingTime');
        return $result;
    }

    /**
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function personalWageLivingRefund(array $param)
    {
        $findYear = (new YearModel())->findYear(date('Y-m-d H:i:s', time()));
        if (empty($findYear)) {
            $findYear['startTime'] = date('Y-01-01 00:00:00');
            $findYear['endTime'] = date('Y-12-31 23:59:59');
        }

        $startTime = empty($param['startTime'])?$findYear['startTime']:$param['startTime'].' 00:00:00';
        $endTime = empty($param['endTime'])?$findYear['endTime']:$param['endTime'].' 23:59:59';

        $condition = [
            'employeeId'=>$param['id'],
            'startTime'=>$startTime,
            'endTime'=>$endTime,
            'type'=>LivingModel::REFUND
        ];
        $data = (new LivingModel())->lists($condition);
        $wageList = $this->datePeriod($startTime, $endTime, '1 month', 'Y-m');
        $endTime = (new \DateTime($endTime))->format('Y-m');
        end($wageList);
        if (key($wageList) != $endTime) {
            $wageList[$endTime] = '';
        }

        $result = $this->wageList($wageList, $data, 'livingRefund', 'livingTime');
        return $result;
    }
}